﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmThuTien : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private bool allowCloseForm = true;
        public static int daluu = 0;
        public static HoaDonBanBO HoaDonBanBO = HoaDonBanBO.NewRootHoaDonBanBO();
        public static HoaDonBanChiTietBOCollection HoaDonBanChiTietBOCollection = HoaDonBanChiTietBOCollection.NewHoaDonBanChiTietBOCollection();


        #endregion variant

        //=====================================
        #region form
        public FrmThuTien()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmThuTien_Load);
            this.FormClosing += new FormClosingEventHandler(FrmThuTien_FormClosing);

            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.spinEditTienKhachTra.EditValueChanged += new System.EventHandler(this.spinEdit1_EditValueChanged);
        }

        private void FrmThuTien_Load(object sender, EventArgs e)
        {
            Utility.InitControl(this.lookUpEditKhachHang);
            Utility.InitControl(this.lookUpGiamDocID);
            Utility.InitControl(this.lookUpKeToanID);
            Utility.InitControl(this.lookUpNguoiGiaoID);
            Utility.InitControl(this.lookUpPhuongThucThanhToanID);

            RefreshData();

        }

        private void FrmThuTien_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control

        private void spinEdit1_EditValueChanged(object sender, EventArgs e)
        {
            double TienKhacTra = StaticUtil.GetDouble(spinEditTienKhachTra.Value, 0);
            double TienThua = TienKhacTra - HoaDonBanBO.TienPhaiThu.Value;
            spinEditTienThua.Value = (decimal)TienThua;
        }

        #endregion control

        //=====================================
        #region button

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            FrmThuTien.daluu = 1;
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid

        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {


            NhanVienBOList nhanVienBOList = NhanVienBOList.GetNhanVienBOList();
            Utility.SetData(this.lookUpGiamDocID, nhanVienBOList, nhanVienBOList.Count);
            Utility.SetData(this.lookUpKeToanID, nhanVienBOList, nhanVienBOList.Count);
            Utility.SetData(this.lookUpNguoiGiaoID, nhanVienBOList, nhanVienBOList.Count);

            DanhMucBOList phuongThucThanhToanBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.PhuongThucThanhToan);
            Utility.SetData(this.lookUpPhuongThucThanhToanID, phuongThucThanhToanBOList, phuongThucThanhToanBOList.Count);

            KhachHangBOList khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.lookUpEditKhachHang, khachHangBOList, khachHangBOList.Count);

            if (HoaDonBanBO.HoaDonBanID != null)
            {
                lookUpEditKhachHang.EditValue = HoaDonBanBO.KhachHangID;
                txtMaSo.Text = HoaDonBanBO.MaSo;
                dateNgayLap.DateTime = HoaDonBanBO.NgayLapHoaDon.Value;
                spinEditTongTien.Value = (decimal)HoaDonBanBO.TienPhaiThu.Value;
                spinEditTienKhachTra.Value = (decimal)HoaDonBanBO.TienKhachTra.Value;
                spinEditTienThua.Value = (decimal)HoaDonBanBO.TienThua.Value;
            }

        }

        private void Save()
        {
            try
            {
                HoaDonBanBOCollection hoaDonBanBOColection = HoaDonBanBOCollection.GetHoaDonBanBOCollection(" convert(nvarchar,NgayLapHoaDon,111) = '" + Session.NgayHeThong.ToString("yyyy/MM/dd") + "'");
                HoaDonBanBO.MaSo = "BH-" + Session.NgayHeThong.ToString("dd/MM/yyyy") + "-" + hoaDonBanBOColection.Count.ToString();
                HoaDonBanBO.HinhThucThanhToanID = StaticUtil.GetInt(lookUpPhuongThucThanhToanID.EditValue, -1);
                HoaDonBanBO.KeToanID = StaticUtil.GetInt(lookUpKeToanID.EditValue, -1);
                HoaDonBanBO.NguoiGiaoHangID = StaticUtil.GetInt(lookUpNguoiGiaoID.EditValue, -1);
                HoaDonBanBO.NguoiNhanHang = txtNguoiNhan.Text.Trim();
                HoaDonBanBO.GiamDocID = StaticUtil.GetInt(lookUpGiamDocID.EditValue, -1);
                HoaDonBanBO.TienKhachTra = StaticUtil.GetDouble(spinEditTienKhachTra.Value);
                HoaDonBanBO.TienThua = HoaDonBanBO.TienKhachTra.Value - HoaDonBanBO.TienPhaiThu.Value;
                if (HoaDonBanBO.TienThua < 0)
                    HoaDonBanBO.TinhTrangThanhToan = -1;
                else
                    HoaDonBanBO.TinhTrangThanhToan = 1;
                HoaDonBanBO.TinhTrangXuatKho = 1;
                HoaDonBanBO.ThanhToan = 1;
                HoaDonBanBO.ThucHien = 2;
                HoaDonBanBO.Save();


                // ----------------------------------------------------------
                #region Xuất khỏi vào Tồn Kho



                int rowCount = HoaDonBanChiTietBOCollection.Count;

                for (int i = 0; i < rowCount; i++)
                {
                    TonKhoBO tonKhoBO = null;
                    int matHangID = StaticUtil.GetInt(HoaDonBanChiTietBOCollection[i].MatHangID, -1);
                    int donViTinhID = StaticUtil.GetInt(HoaDonBanChiTietBOCollection[i].DonViTinhID, -1);
                    int danhMucKhoID = StaticUtil.GetInt(HoaDonBanChiTietBOCollection[i].DanhMucKhoID, -1);
                    tonKhoBO = TonKhoBO.GetRootTonKhoBOByWhere("(MatHangID = " + matHangID.ToString()
                        + ") and (DonViTinhID = " + donViTinhID.ToString()
                        + ") and (DanhMucKhoID = " + danhMucKhoID.ToString() + ")");
                    if (StaticUtil.GetInt(tonKhoBO.TonKhoID.Value, -1) < 0) tonKhoBO = TonKhoBO.NewRootTonKhoBO();
                    tonKhoBO.MatHangID = matHangID;
                    tonKhoBO.DonViTinhID = donViTinhID;
                    tonKhoBO.DanhMucKhoID = danhMucKhoID;
                    tonKhoBO.SoLuongTon = StaticUtil.GetDouble(tonKhoBO.SoLuongTon, 0) - StaticUtil.GetDouble(HoaDonBanChiTietBOCollection[i].SoLuong, 0);
                    tonKhoBO.Save();
                    MaVachBO maVachBO = null;
                    string strWhere = "MaVach = '" + HoaDonBanChiTietBOCollection[i].MaVach.ToString()
                        + "' and TonKhoID = " + StaticUtil.GetInt(tonKhoBO.TonKhoID.Value, -1).ToString();
                    maVachBO = MaVachBO.GetRootMaVachBOByWhere(strWhere);
                    if (StaticUtil.GetInt(maVachBO.MaVachID.Value, -1) < 0) maVachBO = MaVachBO.NewRootMaVachBO();
                    maVachBO.TonKhoID = StaticUtil.GetInt(tonKhoBO.TonKhoID.Value, -1);
                    maVachBO.MaVach = HoaDonBanChiTietBOCollection[i].MaVach.ToString();
                    maVachBO.SoLuongTon = StaticUtil.GetDouble(maVachBO.SoLuongTon, 0) - StaticUtil.GetDouble(HoaDonBanChiTietBOCollection[i].SoLuong, 0);
                    maVachBO.Save();
                }

                #endregion Nhập vào Tồn Kho

            }
            catch
            {

            }
        }
        #endregion function

        private void btnInvaLuu_Click(object sender, EventArgs e)
        {
            Save();
            if (StaticUtil.GetRealInt(HoaDonBanBO.HoaDonBanID.Value, -1) >= 0)
            {
                RHoaDonBanBO rHoaDonBanBO = RHoaDonBanBO.NewRootRHoaDonBanBO();
                rHoaDonBanBO.CopyFromHoaDonBanBO(HoaDonBanBO);
                Report.rpHoaDonBanHang rHoaDonBanHang = new TanHungLong.Report.rpHoaDonBanHang(rHoaDonBanBO, HoaDonBanChiTietBOCollection);
                rHoaDonBanHang.ShowPreviewDialog();
            }
            FrmThuTien.daluu = 1;
            this.Close();
        }
    }
}